package com.tjs.lucky.infrastructure.repository;

import com.tjs.lucky.common.ActivityState;
import com.tjs.lucky.common.RedisKey;
import com.tjs.lucky.common.ResponseCode;
import com.tjs.lucky.domain.activity.model.req.PartakeReq;
import com.tjs.lucky.domain.activity.model.res.StockResult;
import com.tjs.lucky.domain.activity.model.vo.*;
import com.tjs.lucky.domain.activity.repository.IActivityRepository;
import com.tjs.lucky.infrastructure.mapper.*;
import com.tjs.lucky.infrastructure.po.*;
import com.tjs.lucky.infrastructure.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 田佳澍
 * @Description:
 * @DateTime: 2023/5/22 14:58
 **/
@Repository
@Slf4j
public class ActivityRepository implements IActivityRepository {
    @Resource
    private IActivityMapper activityMapper;
    @Resource
    private IAwardMapper awardMapper;
    @Resource
    private IStrategyMapper strategyMapper;
    @Resource
    private IStrategyDetailMapper strategyDetailMapper;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private RedissonClient redissonClient;

    @Override
    public void addActivity(ActivityVO activity) {
        Activity req = new Activity();
        BeanUtils.copyProperties(activity, req);
        req.setStockSurplusCount(activity.getStockCount());
        activityMapper.insert(req);
    }

    @Override
    public void addAward(List<AwardVO> awardList) {
        List<Award> req = new ArrayList<>();
        for (AwardVO awardVO : awardList) {
            Award award = new Award();
            BeanUtils.copyProperties(awardVO, award);
            req.add(award);
        }
        awardMapper.insertList(req);
    }

    @Override
    public void addStrategy(StrategyVO strategy) {
        Strategy req = new Strategy();
        BeanUtils.copyProperties(strategy, req);
        strategyMapper.insert(req);
    }

    @Override
    public void addStrategyDetailList(List<StrategyDetailVO> strategyDetailList) {
        List<StrategyDetail> req = new ArrayList<>();
        for (StrategyDetailVO strategyDetailVO : strategyDetailList) {
            StrategyDetail strategyDetail = new StrategyDetail();
            BeanUtils.copyProperties(strategyDetailVO, strategyDetail);
            strategyDetail.setAwardSurplusCount(strategyDetailVO.getAwardCount());
            req.add(strategyDetail);
        }
        strategyDetailMapper.insertList(req);
    }

    @Override
    public boolean alterStatus(Long activityId, Enum<ActivityState> beforeState, Enum<ActivityState> afterState) {
        AlterStateVO alterStateVO = new AlterStateVO(activityId, ((ActivityState) beforeState).getCode(), ((ActivityState) afterState).getCode());
        int count = activityMapper.alterState(alterStateVO);
        return 1 == count;
    }

    @Override
    public ActivityBillVO queryActivityBill(PartakeReq req) {

        // 查询活动信息
        Activity activity = activityMapper.queryActivityByActivityId(req.getActivityId());

        // 封装结果信息
        ActivityBillVO activityBillVO = new ActivityBillVO();

        // 规则树Id
        activityBillVO.setTreeId(activity.getTreeId());
        //用户Id
        activityBillVO.setUId(req.getUId());
        //活动Id
        activityBillVO.setActivityId(req.getActivityId());
        //活动名称
        activityBillVO.setActivityName(activity.getActivityName());
        //起始时间
        activityBillVO.setBeginDateTime(activity.getBeginDateTime());
        activityBillVO.setEndDateTime(activity.getEndDateTime());
        //活动库存
        activityBillVO.setStockCount(activity.getStockCount());
        //库存剩余
        activityBillVO.setStockSurplusCount(activity.getStockSurplusCount());
        //策略Id
        activityBillVO.setStrategyId(activity.getStrategyId());
        //状态
        activityBillVO.setState(activity.getState());
        //用户剩余参与次数
        activityBillVO.setUserTakeLeftCount(activity.getTakeCount());

        return activityBillVO;
    }

    @Override
    public int subtractionActivityStock(Long activityId) {
        return activityMapper.subtractionActivityStock(activityId);
    }


    @Override
    public void synActivityStockJob(String currStock){
        Activity activity = new Activity();
        activity.setStockCount(Integer.valueOf(currStock));
        activityMapper.update(activity);
    }

    @Override
    public List<ActivityVO> scanToDoActivityList(Long id) {
        List<Activity> activityList = activityMapper.scanToDoActivityList(id);
        List<ActivityVO> activityVOList = new ArrayList<>(activityList.size());
        for (Activity activity : activityList) {
            ActivityVO activityVO = new ActivityVO();
            activityVO.setActivityId(activity.getId());
            activityVO.setActivityId(activity.getActivityId());
            activityVO.setActivityName(activity.getActivityName());
            activityVO.setBeginDateTime(activity.getBeginDateTime());
            activityVO.setEndDateTime(activity.getEndDateTime());
            activityVO.setState(activity.getState());
            activityVOList.add(activityVO);
        }
        return activityVOList;
    }

    @Override
    public ActivityVO queryActivityInfo(Long activityId) {
        ActivityVO activityVO = new ActivityVO();
        Activity activity = activityMapper.queryActivityByActivityId(activityId);
        BeanUtils.copyProperties(activity, activityVO);
        return activityVO;
    }

}
